package com.briup.cms.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * 工具类，负责登录后，用户访问的时候保存当前访问的信息和用户信息
 * 使用threadLocal实现
 * @author wood
 */
public class SecurityUtils {

    private static ThreadLocal<Map<String,String>> threadLocal = new ThreadLocal<>();

    public static String get(String key){
        if(threadLocal.get()==null){
            return null;
        }
        return threadLocal.get().get(key);
    }

    public static void setMap(Map<String,String> map){
        threadLocal.set(map);
    }


    public static void setKV(String key,String value){
        if(threadLocal.get()==null){
            Map<String,String> map = new HashMap<>();
            threadLocal.set(map);
        }
        threadLocal.get().put(key,value);
    }

    public static void setmethod(String method){
        setKV("method",method);
    }
    public static String getmethod(){
        return get("method");
    }

    public static void setRequestURI(String requestURI) {
        setKV("requestURI",requestURI);
    }
    public static String getRequestURI(){
        return get("requestURI");
    }

    public static void setUsername(String username) {
        setKV("username",username);
    }
    public static String getUsername(){
        return get("username");
    }

    public static void setRealname(String realname) {
        setKV("realname",realname);
    }
    public static String getRealname(){
        return get("realname");
    }
}
